package com.weixin.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
//@WebFilter("/*")
public class LoginFilter implements Filter{

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest requ = (HttpServletRequest)request;
		//得到访问路径：
		String uri = requ.getRequestURI();
		if( (uri.contains("jsp")&&!uri.endsWith("login.jsp")) || (uri.contains("Servlet")&&!uri.endsWith("User_TbServlet/login"))||uri.endsWith(requ.getContextPath()+"/") ) {
			//需要验证登录状态的路径
			HttpSession session = requ.getSession();
			//session.getAttribute("user");
			
			if(session.getAttribute("user")!=null) {
				//已经登录
				chain.doFilter(request, response);
			}else {
				//没有登录
				requ.setAttribute("errMsg", "请先登录");
				request.getRequestDispatcher("/login.jsp").forward(request, response);
			}
		}else {
			//直接放行的路径：
			chain.doFilter(request, response);
		}
	}
	}